package com.mallplus.rbac.controller;

import com.mallplus.api.auth.bo.UserInfoInTokenBO;
import com.mallplus.common.core.response.ServerResponseEntity;
import com.mallplus.common.core.util.BooleanUtil;
import com.mallplus.common.security.AuthUserContext;
import com.mallplus.rbac.model.Menu;
import com.mallplus.rbac.service.MenuService;
import com.mallplus.rbac.vo.MenuSimpleVO;
import com.mallplus.rbac.vo.MenuVO;
import com.mallplus.rbac.vo.RouteMetaVO;
import com.mallplus.rbac.vo.RouteVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author Hanson
 * @date 2023/4/17 15:36
 */
@RequestMapping(value = "/menu")
@RestController
@Tag(name = "菜单接口")
public class MenuController {

    @Resource
    private MenuService menuService;

    @GetMapping(value = "/route")
    @Operation(summary = "路由菜单" , description = "获取当前登陆用户可用的路由菜单列表")
    public ServerResponseEntity<List<RouteVO>> route(Integer sysType) {
        sysType = Objects.isNull(sysType) ? AuthUserContext.get().getSysType(): sysType;
        List<Menu> menus = menuService.listBySysType(sysType);

        List<RouteVO> routes = new ArrayList<>(menus.size());

        for (Menu menu : menus) {
            RouteVO route = new RouteVO();
            route.setAlwaysShow(BooleanUtil.isTrue(menu.getAlwaysShow()));
            route.setComponent(menu.getComponent());
            route.setHidden(BooleanUtil.isTrue(menu.getHidden()));
            route.setName(menu.getName());
            route.setPath(menu.getPath());
            route.setRedirect(menu.getRedirect());
            route.setId(menu.getMenuId());
            route.setParentId(menu.getParentId());
            route.setSeq(menu.getSeq());

            RouteMetaVO meta = new RouteMetaVO();
            meta.setActiveMenu(menu.getActiveMenu());
            meta.setAffix(BooleanUtil.isTrue(menu.getAffix()));
            meta.setBreadcrumb(BooleanUtil.isTrue(menu.getBreadcrumb()));
            meta.setIcon(menu.getIcon());
            meta.setNoCache(BooleanUtil.isTrue(menu.getNoCache()));
            meta.setTitle(menu.getTitle());
            // 对于前端来说角色就是权限
            meta.setRoles(Collections.singletonList(menu.getPermission()));

            route.setMeta(meta);
            routes.add(route);
        }
        return ServerResponseEntity.success(routes);
    }

    @GetMapping
    @Operation(summary = "获取菜单管理" , description = "根据menuId获取菜单管理")
    public ServerResponseEntity<MenuVO> getByMenuId(@RequestParam Long menuId) {
        return ServerResponseEntity.success(menuService.getByMenuId(menuId));
    }

    @GetMapping(value = "/list_with_permissions")
    @Operation(summary = "菜单列表和按钮列表" , description = "根据系统类型获取该系统的菜单列表 + 菜单下的权限列表")
    public ServerResponseEntity<List<MenuSimpleVO>> listWithPermissions() {
        UserInfoInTokenBO userInfoInTokenBO = AuthUserContext.get();
        List<MenuSimpleVO> menuList = menuService.listWithPermissions(userInfoInTokenBO.getSysType());
        return ServerResponseEntity.success(menuList);
    }
}
